﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblCustomersDAO
    {
        private DBService _db = null;
        public TblCustomersDAO(String StringConnection)
        {
            _db = new DBService(StringConnection);
        }

        //insert customer

        public DataTable Insert_Up(TblCustomers customer, int mode)
        {
            DataTable dt = null;

            try
            {

                dt = _db.ExeSP_DataTable("sp_TblCustomers_InsUpd",
                    new String[] { "@FName", "@LName", "@Company", "@CusAddress", "@EmailAdress",
	                                        "@Password", "@HomePhone", "@WorkPhone", "@MobilePhone", "@Mode", "@IsAdmin"},
                    new object[] { customer.FName, customer.LName, customer.Company, customer.CusAddress, customer.EmailAdress, customer.Password, 
                                   customer.HomePhone, customer.WorkPhone, customer.MobilePhone, customer.IsAdmin ,mode }, CommandType.StoredProcedure);


            }
            catch (Exception e)
            {
                throw e;
            }
            return dt;
        }

        public bool CheckExits(string mail, int mode)
        {
            bool flag = false;
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_CheckEmail",
                    new string[] { "@Email", "@mode" },
                    new object[] { mail, mode },
                    CommandType.StoredProcedure);
                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }


        public TblCustomers GetAdmin(string Email)
        {
            TblCustomers user = new TblCustomers();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_AdminCheck",
                   new string[] { "@Email" },
                   new object[] { Email },
                  CommandType.StoredProcedure);

                foreach (DataRow dr in dt.Rows)
                {
                    user.EmailAdress = dr["EmailAdress"].ToString();
                    user.CusID = Convert.ToInt32(dr["CusID"]);
                    user.FName = dr["FName"].ToString();
                    user.LName = dr["LName"].ToString();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return user;
        }

        public TblCustomers GetUser(string emailaddress)
        {
            TblCustomers user = new TblCustomers();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("sp_TblUserAccount_Check",
                   new string[] { "@Email" },
                   new object[] { emailaddress },
                  CommandType.StoredProcedure);

                foreach (DataRow dr in dt.Rows)
                {
                    user.CusID = Convert.ToInt32(dr["CusID"].ToString());
                    user.EmailAdress = dr["EmailAdress"].ToString();
                    user.FName = dr["FName"].ToString();
                    user.LName = dr["LName"].ToString();
                    user.MobilePhone = dr["MobilePhone"].ToString();

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return user;
        }

    }
}
